
<!DOCTYPE html>
<html lang="zh-cn">
    
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <meta name="msvalidate.01" content="41F40C345D497DB6D4AFA53DAE9B225E" />
  
    <link rel="icon" href="/img/favicon.ico">
  
  
      <meta name="author" content="东南dnf">
  
  
  
      <meta name="description" content="东南dnf的个人博客">
  
  
  
    <link rel="alternate" href="/atom.xml " title="东南dnf的博客" type="application/atom+xml">
  

  

  <title>MFC操作Sqlite数据库 | 东南dnf的博客</title>

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?13d6b1f7cf31ea3697af89bead105483";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
      
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>


  

  <link rel="stylesheet" href="/css/style.css" >
  <link rel="stylesheet" href="/css/partial/dark.css" >

  
  
  

  
    
      <link rel="stylesheet" href="/css/partial/highlight/atom-one-light.css">
    
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css">
    
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css">
    
      <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/a2396837/CDN@latest/css/iconfont.css">
    
  

  
    <script src="/js/todark.js"></script>
    
<meta name="generator" content="Hexo 6.3.0"></head>

    
<div class="nav index" style="height: 60px;">
    <div class="title animated fadeInDown">
        <div class="layui-container">
                <div class="nav-title"><a href="/" title="东南dnf的博客">东南dnf的博客</a></div>
            <div class="nav-list">
                <button> <span class=""></span><span style="display: block;"></span><span class=""></span> </button>
                <ul class="layui-nav" lay-filter="">
                    
                        
                        
                        
                        
                    <li class="layui-nav-item">
                        <a href="/ ">
                            <i class=" fab fa-fort-awesome " style="color: rgb(255 107 107);"></i>
                            <span class="layui-nav-item-name">主页</span>
                        </a>
                    </li>
                    
                        
                        
                        
                        
                    <li class="layui-nav-item">
                        <a href="/archives/ ">
                            <i class=" fas fa-archive " style="color: rgb(10 189 227);"></i>
                            <span class="layui-nav-item-name">归档</span>
                        </a>
                    </li>
                    
                        
                        
                        
                        
                    <li class="layui-nav-item">
                        <a href="/tags/ ">
                            <i class=" fas fa-hashtag " style="color: rgb(254 202 87);"></i>
                            <span class="layui-nav-item-name">标签</span>
                        </a>
                    </li>
                    
                        
                        
                        
                        
                    <li class="layui-nav-item">
                        <a href="/categories ">
                            <i class=" far fa-folder-open " style="color: rgb(29 209 161);"></i>
                            <span class="layui-nav-item-name">分类</span>
                        </a>
                    </li>
                    
                        
                        
                        
                        
                    <li class="layui-nav-item">
                        <a href="/search/ ">
                            <i class=" fas fa-search-plus " style="color: rgb(3 169 244);"></i>
                            <span class="layui-nav-item-name">搜索</span>
                        </a>
                    </li>
                    
                        
                        
                        
                        
                    <li class="layui-nav-item">
                        <a href="/link/ ">
                            <i class=" fab fa-weixin " style="color: hsl(152deg 73% 45%);"></i>
                            <span class="layui-nav-item-name">友链</span>
                        </a>
                    </li>
                    
                        
                        
                        
                        
                    <li class="layui-nav-item">
                        <a href="/guestbook/ ">
                            <i class=" fab fa-telegram " style="color: hsl(205deg 100% 50%);"></i>
                            <span class="layui-nav-item-name">留言</span>
                        </a>
                    </li>
                    
                        
                        
                        
                        
                    <li class="layui-nav-item">
                        <a href="/about/ ">
                            <i class=" fab fa-grav " style="color: rgb(154 106 247);"></i>
                            <span class="layui-nav-item-name">关于</span>
                        </a>
                    </li>
                    
                    
                        <li class="layui-nav-item" id="btn-toggle-dark">🌙</li>
                    
                    <span class="layui-nav-bar" style="left: 342px; top: 78px; width: 0px; opacity: 0;"></span>
                </ul>
            </div>
        </div>
    </div>
</div>
    
<header class="header">
        
            <div class="logo">
                    <a href="/"><img src="https://lib.dnf.doyi.online/imgs/avatar/dnf-avatar.jpeg" onerror=this.onerror=null,this.src="/img/loading.gif"></a>
            </div>
         
    </div>
     
    
            <div class="social">
                
                        <a class="social-icon" href="https://github.com/zjy2414" target="_blank" title="Github">
                            <i class="iconfont icon-GitHub" aria-hidden="true"></i>
                          </a>
                 
                        <a class="social-icon" href="mailto:zjy2414@outlook.com" target="_blank" title="Email">
                            <i class="iconfont icon-email" aria-hidden="true"></i>
                          </a>
                 
                        <a class="social-icon" href="https://space.bilibili.com/354104366" target="_blank" title="Bilibili">
                            <i class="iconfont icon-bilibili" aria-hidden="true"></i>
                          </a>
                 
                        <a class="social-icon" href="/atom.xml" target="_blank" title="rss">
                            <i class="iconfont icon-rss" aria-hidden="true"></i>
                          </a>
                 
            </div>
     
</header>

    
<article id="post">
  <div class="post-title">MFC操作Sqlite数据库</div>
  
<div class="post-meta">
    
    
      <div class="post-meta-item date">
        <span title="Created 2021.07.05"><i class="far fa-calendar-alt"></i> 2021.07.05</span>
      </div>
      <div class="post-meta-item updated">
        <span title="Updated 2023.09.05"><i class="far fa-calendar-check"></i> 2023.09.05</span>
      </div>
     
    
      <div class="post-meta-item categories">
        
          <i class="fas fa-inbox article-meta__icon"></i> <a href="/categories/technology/">技术</a>
        
      </div>
     
    
     <div class="post-meta-item wordcount">
        
          <i class="fas fa-pencil-alt"></i> <span class="post-count">481 words</span>
           
        
          <i class="far fa-clock"></i> <span class="post-count">2 min</span>
                               
      </div>
     
</div>


  
    <div id="toc" class="toc">
          <h1>Toc</h1>
          <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BDSqlite%E5%AE%98%E6%96%B9%E7%9A%84dll%E5%92%8Cdef%E6%96%87%E4%BB%B6%E5%8F%8A%E6%BA%90%E4%BB%A3%E7%A0%81"><span class="toc-number">1.</span> <span class="toc-text">下载Sqlite官方的dll和def文件及源代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%A9%E7%94%A8lib%E5%91%BD%E4%BB%A4%E7%BC%96%E8%AF%91lib%E6%96%87%E4%BB%B6%EF%BC%88lib-exe%E9%80%9A%E5%B8%B8%E5%9C%A8Visual-Studio%E7%9A%84%E6%9F%90%E4%B8%AA%E5%AE%89%E8%A3%85%E7%9B%AE%E5%BD%95%E4%B8%8B%EF%BC%89"><span class="toc-number">2.</span> <span class="toc-text">利用lib命令编译lib文件（lib.exe通常在Visual Studio的某个安装目录下）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95%E4%B8%80%E4%B8%8B"><span class="toc-number">3.</span> <span class="toc-text">测试一下</span></a></li></ol>
      </div>
    
  <div class="content">
        <div><p>这几天为了完成小学期面向对象程序设计的大作业不得不捣鼓MFC。<br>But，MFC相关的资料少之又少，不少资料甚至还停留在VC++ 6.0的时代。</p>
<h2 id="下载Sqlite官方的dll和def文件及源代码"><a href="#下载Sqlite官方的dll和def文件及源代码" class="headerlink" title="下载Sqlite官方的dll和def文件及源代码"></a>下载Sqlite官方的dll和def文件及源代码</h2><p>链接: <a target="_blank" rel="noopener" href="https://sqlite.org/download.html">https://sqlite.org/download.html</a></p>
<h2 id="利用lib命令编译lib文件（lib-exe通常在Visual-Studio的某个安装目录下）"><a href="#利用lib命令编译lib文件（lib-exe通常在Visual-Studio的某个安装目录下）" class="headerlink" title="利用lib命令编译lib文件（lib.exe通常在Visual Studio的某个安装目录下）"></a>利用lib命令编译lib文件（lib.exe通常在Visual Studio的某个安装目录下）</h2><p>先将刚刚下载的2个压缩包中的文件解压到同一个文件夹，然后执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">lib /def:sqlite3.def /machine:x64 /out:sqlite3.lib</span><br></pre></td></tr></table></figure>

<p>执行结果如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">PS D:\sqlite-dll-win64-x64&gt; lib /def:sqlite3.def /machine:x64 /out:sqlite3.lib</span><br><span class="line">Microsoft (R) Library Manager Version 14.29.30040.0</span><br><span class="line">Copyright (C) Microsoft Corporation.  All rights reserved.</span><br><span class="line"></span><br><span class="line">  正在创建库 sqlite3.lib 和对象 sqlite3.exp</span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">PS D:\sqlite-dll-win64-x64&gt; <span class="built_in">ls</span></span><br><span class="line"></span><br><span class="line">    目录: D:\sqlite-dll-win64-x64&gt;</span><br><span class="line"></span><br><span class="line">Mode                 LastWriteTime         Length Name</span><br><span class="line">----                 -------------         ------ ----</span><br><span class="line">-a----         2021/6/19      2:52         687509 shell.c</span><br><span class="line">-a----         2021/6/19      2:52        8312766 sqlite3.c</span><br><span class="line">-a----         2021/6/19      7:23           7568 sqlite3.def</span><br><span class="line">-a----         2021/6/19      7:23        2127360 sqlite3.dll</span><br><span class="line">-a----          2021/9/2     18:16          46811 sqlite3.exp</span><br><span class="line">-a----         2021/6/19      2:52         588809 sqlite3.h</span><br><span class="line">-a----          2021/9/2     18:16          77424 sqlite3.lib</span><br><span class="line">-a----         2021/6/19      2:52          35437 sqlite3ext.h</span><br></pre></td></tr></table></figure>

<p>可以看到lib文件已经正常生成了。</p>
<p>然后为项目引入sqlite3.dll,sqlite3.def和sqlite3.h文件</p>
<h2 id="测试一下"><a href="#测试一下" class="headerlink" title="测试一下"></a>测试一下</h2><p>添加button控件</p>
<p>给按钮添加响应事件</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="type">void</span> <span class="title">CMFCApplication2Dlg::OnBnClickedButton1</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line"> <span class="comment">// 初始化数据库</span></span><br><span class="line"> sqlite3* m_sql_database;</span><br><span class="line"> <span class="type">int</span> rc;</span><br><span class="line"> <span class="type">char</span>* err_msg = <span class="literal">NULL</span>;</span><br><span class="line"> <span class="type">char</span> sql[<span class="number">200</span>] = <span class="string">&quot;&quot;</span>;</span><br><span class="line"></span><br><span class="line"> rc = <span class="built_in">sqlite3_open</span>(<span class="string">&quot;./data.db&quot;</span>, &amp;m_sql_database);<span class="comment">// 打开数据库，不存在时创建</span></span><br><span class="line"> <span class="keyword">if</span> (rc)</span><br><span class="line"> &#123;</span><br><span class="line">  <span class="built_in">sqlite3_close</span>(m_sql_database);  <span class="comment">//打开失败！ </span></span><br><span class="line">  <span class="built_in">MessageBox</span>(_T(<span class="string">&quot;数据库打开失败，请检查后再操作！&quot;</span>), <span class="literal">NULL</span>, MB_ICONSTOP);</span><br><span class="line">  <span class="keyword">return</span>;</span><br><span class="line"> &#125;</span><br><span class="line"> <span class="built_in">sprintf_s</span>(sql, <span class="string">&quot;CREATE TABLE IF NOT EXISTS test(id int PRIMARY KEY NOT NULL, item varchar(20) NOT NULL UNIQUE)&quot;</span>);<span class="comment">// 创建一个表格</span></span><br><span class="line"> <span class="keyword">if</span> (<span class="built_in">sqlite3_exec</span>(m_sql_database, sql, <span class="literal">NULL</span>, <span class="literal">NULL</span>, &amp;err_msg) != SQLITE_OK)</span><br><span class="line"> &#123;</span><br><span class="line">  <span class="built_in">MessageBox</span>(_T(<span class="string">&quot;Error!&quot;</span>), <span class="literal">NULL</span>, MB_ICONSTOP);</span><br><span class="line">  <span class="keyword">return</span>;</span><br><span class="line"> &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>编译运行，点击按钮，可以看到当前目录下已经生成了data.db文件，说明数据库已经成功创建了。</p>
</div>
        
          <div class="post-copyright">
                  <div class="copyright-item">
                      <span> Author: 东南dnf</span>
                  </div>
                  <div class="copyright-item">
                      <span> Link: <a href="https://dnf.doyi.online/posts/114706478.html">https://dnf.doyi.online/posts/114706478.html</a></span>
                  </div>
                  <div class="copyright-item">
                      <span> License: 本博客所有文章除特别声明外，均采用许可协议 <a target="_blank" rel="noopener" href="http://creativecommons.org/licenses/by-nc/4.0/">CC-BY-NC-4.0</a> 转载请注明出处！</span>
                  </div>
          </div>
        
  </div>

  <div class="share-reward">
    <div class="share">
        
<div class="social-share" data-sites="wechat,weibo,qq"></div>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css">
<script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js"></script>


      </div>
        <div class="reward">
          
  <div style="padding: 10px 0; margin-left: 20px; width: 90%; text-align: center;">
    <div class="reward-content">
        <ul id="donateBox" class="list pos-a">
          
           <li id="PayPal"><a href="https://paypal.me/DongNanDNF" target="_blank" title="PayPal">PayPal</a></li>
            
          
            <li id="AliPay" title="Alipay"><img data-img="/img/alipay.jpg" onerror=this.onerror=null,this.src="/img/loading.gif"></img></li>
               
                 
            <li id="WeChat" title="Wechatpay"><img date-img="/img/wechat.jpg" onerror=this.onerror=null,this.src="/img/loading.gif"></img></li>
                      
          </ul>
        <div id="QRBox" class="pos-f left-100">
          <div id="MainBox"></div>
        </div>
    </div>
  </div>
  
        </div>
    </div>
    
    <div class="post_tags">
      
        <i class="fas fa-tag"></i> <a href="/tags/MFC/" class="tag">MFC</a>
      
        <i class="fas fa-tag"></i> <a href="/tags/Sqlite/" class="tag">Sqlite</a>
      
    </div>
    <div class="post-nav">
      
        <div class="post-nav-prev post-nav-item">
            <a href="/posts/3963924866.html" >常见排序算法及其Golang实现<i class="fa fa-chevron-left"></i></a>
        </div>
      
      
        <div class="post-nav-next post-nav-item">
            <a href="/posts/1070717659.html" >docker升级报错containerd.io &gt;= 1.4.1的解决方案<i class="fa fa-chevron-right"></i></a>
        </div>
      
    </div>
      



  <div id="valine"></div>
  <script src="https://cdn.jsdelivr.net/npm/valine/dist/Valine.min.js"></script>
  <script>
        new Valine({
          el: '#valine',
          appId: "afx5w8dIibpY5M5h2fLUHKfY-gzGzoHsz",
          appKey: "Hyp6VXPvgQxF1PTysvcjleQs",
          avatar: "mm",
          lang: "",
          meta: 'nick,mail,link'.split(','),
          requiredFields: 'nick,mail'.split(','),
          placeholder: "评论记得带上邮箱,你的留言我会马上收到邮箱提醒哒",
          pageSize:'10',
          recordIP: 'false',
          serverURLs: "",
          emojiCDN: "",
          enableQQ: "true",
        });
  </script>
  

</article>

    
<a id="gotop" href="javascript:" title="返回顶部"><i class="fa fa-arrow-up"></i></a>
    






    
<div id="bottom-outer">
    <div id="bottom-inner">
        © 2023 东南dnf All Rights Reserved.
        
        <br>

        
          <div class="icp-info">
            
            <img  class="icp-icon" src="/img/icp.png">
            
          <a href="https://beian.miit.gov.cn/" target="_blank"> 沪ICP备14039016号-17</a>
        </div>
        

        
    </div>  
  </div>
  
<script src="https://cdn.jsdelivr.net/npm/layui-src@2.5.5/dist/layui.min.js"></script>



  
    <script src="/js/script.js"></script>
  
    <script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script>
  

 

  <script src="/js/local-search.js"></script>
<script type="text/javascript">      
  var search_path = "search.xml";
if (search_path.length == 0) {
  search_path = "search.xml";
}
var path = "/" + search_path;
  searchFunc(path, 'local-search-input', 'local-search-result');
</script>
  


  <script>
    window.lazyLoadOptions = {
      elements_selector: 'img',
      threshold: 0
    }
</script>
<script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script>   
  


  <script>
    var images = $('img').not('.nav-logo img').not('.card img').not($('a>img')).not('.reward-content img')
    images.each(function (i, o) {
      var lazyloadSrc = $(o).attr('data-src') ? $(o).attr('data-src') : $(o).attr('src')
      $(o).wrap(`<a href="${lazyloadSrc}" data-fancybox="group" data-caption="${$(o).attr('alt')}" class="fancybox"></a>`)
    })
  </script>
  <script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script>
  <script>
        $().fancybox({
      selector: '[data-fancybox]',
      loop: true,
      transitionEffect: 'slide',
      protect: true,
      buttons: ['slideShow', 'fullScreen', 'thumbs', 'close']
    })
  </script>   
  






  <script>
    jQuery(document).ready(function () {
      var QRBox = $('#QRBox');
      var MainBox = $('#MainBox');
      var AliPayQR = '/img/alipay.jpg';
      var WeChanQR = '/img/wechat.jpg';

      function showQR(QR) {
        if (QR) {
          MainBox.css('background-image', 'url(' + QR + ')');
        }
        $('#donateBox').addClass('blur');
        QRBox.fadeIn(300, function (argument) {
          MainBox.addClass('showQR');
        });
      }

      $('#donateBox>li').click(function (event) {
        var thisID = $(this).attr('id');
        if (thisID === 'AliPay') {
          showQR(AliPayQR);
        } else if (thisID === 'WeChat') {
          showQR(WeChanQR);
        }
      });

      MainBox.click(function (event) {
        MainBox.removeClass('showQR').addClass('hideQR');
        setTimeout(function (a) {
          QRBox.fadeOut(300, function (argument) {
            MainBox.removeClass('hideQR');
          });
          $('#DonateText,#donateBox,#github').removeClass('blur');
        }, 600);

      });
    });
  </script>
  




  <script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module" defer></script>
  




  
<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.4/dist/clipboard.min.js"></script>
<script>
!function (e, t, a) {
  var initCopyCode = function(){
    var copyHtml = '';
    copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
    copyHtml += '  <i class="fa fa-clipboard"></i><span>复制</span>';
    copyHtml += '</button>';
    $(".highlight .code pre").before(copyHtml);
    new ClipboardJS('.btn-copy', {
      target: function(trigger) {
        return trigger.nextElementSibling;
      }
    });
  }
  initCopyCode();
}(window, document);
</script>  
  

<script>
  var btntop = $('#gotop');
  btntop.on('click', function (e) {
    e.preventDefault();
    $('html, body').animate({ scrollTop: 0 }, '300');
  });

  var $table = $('.content table').not($('figure.highlight > table'))
$table.each(function () {
  $(this).wrap('<div class="table-wrap"></div>')
})
</script>



</html>